package com.wydpp.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.wydpp.domain.Device;
import com.wydpp.mapper.DeviceMapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeviceRepository extends CrudRepository<DeviceMapper, Device> {

    /**
     * 根据设备id查询设备
     *
     * @param deviceId
     * @return
     */
    public Device findDeviceByDeviceId(String deviceId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<Device>().eq(Device::getDeviceId, deviceId).last(" limit 1"));
    }

    /**
     * 查询所有已经注册的设备
     *
     * @return
     */
    public List<Device> findRegisteredDevice() {
        List<Device> devices = baseMapper.selectList(new LambdaQueryWrapper<Device>()
                .eq(Device::getRegistered, 1));
        return devices;
    }

    /**
     * 查询所有需要注册的设备
     *
     * @return
     */
    public List<Device> findToBeRegisteredDevice() {
        List<Device> devices = baseMapper.selectList(new LambdaQueryWrapper<Device>()
                .eq(Device::getNeedRegister, 0)
                .eq(Device::getRegistered, 0));
        return devices;
    }

}
